package com.geekchique.runningbuddy.model;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.TreeMap;

import android.location.Location;

import com.geekchique.runningbuddy.util.DBHelperImpl;
import com.geekchique.runningbuddy.util.DBHelperTools;

public class Run {
	public long						_id;
	public Route					route;
	public String					name;
	public long						startTime;
	public long						stopTime;
	public Calendar					startTimeCal;
	public Calendar					stopTimeCal;
	private Map<Long, Location>		locationsAndTimes;
	private DBHelperImpl			dbHelper;
	private long					createdAt;
	private long					finishAt;

	private boolean					syncDatabase	= false;
	private LinkedList<RunSegment>	runSegmentList;
	private int						id;

	//    
	// public Run() {
	// }
	//
	// public Run(long _id, String name, long startTime, long stopTime)
	// {
	// this._id = _id;
	// this.name = name;
	// this.startTime = startTime;
	// this.stopTime = stopTime;
	// this.runSegmentList = new LinkedList<RunSegment>();
	//		
	// }
	//	
	// public Run(String name, long startTime, long stopTime)
	// {
	// this.name = name;
	// this.startTime = startTime;
	// this.stopTime = stopTime;
	// locationsAndTimes = new TreeMap<Long, Location>();
	// this.runSegmentList = new LinkedList<RunSegment>();
	// }
	//
	// public Run(String name, String route, TreeMap<Long, Location>
	// locationsAndTimes) {
	// this.name = name;
	// this.route = route;
	// this.locationsAndTimes = locationsAndTimes;
	// this.startTime = locationsAndTimes.firstKey();
	// this.stopTime = locationsAndTimes.lastKey();
	// this.runSegmentList = new LinkedList<RunSegment>();
	// }
	//
	// // This is the preferred constructor when starting a jog. It creates the
	// entries in the database.
	// public Run(DBHelperImpl dbHelper, String name, long startTime)
	// {
	// this.name = name;
	// this.startTime = startTime;
	// this.createInDatabase(dbHelper);
	// this.locationsAndTimes = new TreeMap<Long, Location>();
	// this.runSegmentList = new LinkedList<RunSegment>();
	// syncDatabase = true;
	// }
	//
	//
	//
	public Run(int runId, Route route, String runName, long runTotalTime, long runCreation) {
		this.id = runId;
		this.route = route;
		this.name = runName;
		// this.createInDatabase(dbHelper);
		this.locationsAndTimes = new TreeMap<Long, Location>();
		this.runSegmentList = new LinkedList<RunSegment>();
		syncDatabase = true;
	}

	public Run(int runId, Route route, String runName, LinkedList<RunSegment> runSegments, long runTotalTime, long runCreation) {
		this.id = runId;
		this.route = route;
		this.name = runName;
		// this.createInDatabase(dbHelper);
		this.locationsAndTimes = new TreeMap<Long, Location>();
		this.runSegmentList = runSegments;
		syncDatabase = true;

	}

	public Calendar getStartTimeCal() {
		if (startTimeCal == null) {
			Calendar cal = new GregorianCalendar();
			cal.setTimeInMillis(startTime);
			return startTimeCal = cal;
		}
		return startTimeCal;
	}

	public void setStartTimeCal(Calendar startTimeCal) {
		this.startTimeCal = startTimeCal;
	}

	public Calendar getStopTimeCal() {
		if (stopTimeCal == null) {
			Calendar cal = new GregorianCalendar();
			cal.setTimeInMillis(stopTime);
			return stopTimeCal = cal;
		}
		return stopTimeCal;
	}

	public void setStopTimeCal(Calendar stopTimeCal) {
		this.stopTimeCal = stopTimeCal;
	}

	public void setStopTime(long stopTime) {
		this.stopTime = stopTime;
		if (syncDatabase) {

		}
	}

	public double getDistance() {
		if (locationsAndTimes == null)
			return 0.0;

		double distance = 0.0;

		Location loc = null;

		Iterator<Map.Entry<Long, Location>> it = locationsAndTimes.entrySet().iterator();
		while (it.hasNext()) {
			Location nextLoc = it.next().getValue();
			if (loc != null) {
				distance += loc.distanceTo(nextLoc);
			}

			loc = nextLoc;

		}

		return distance;

	}

	public void addRunSegment(RunSegment runSeg) {
		this.runSegmentList.add(runSeg);
	}

	public LinkedList<RunSegment> getRunSegments() {
		return this.runSegmentList;
	}

	public String getStartDateTimeOfRun() {
		return DBHelperTools.isoFormat(this.createdAt);
	}

	public String getEndDateTimeOfRun() {
		return DBHelperTools.isoFormat(this.finishAt);
	}

	public String getStartTime() {
		return DBHelperTools.isoFormat(this.startTime);
	}

	public String getStopTime() {
		return DBHelperTools.isoFormat(this.stopTime);
	}

	public void addFirstRunSegment(RunSegment runSeg) {
		this.startTime = runSeg.createdAt;
		this.runSegmentList.add(runSeg);
	}

	public void addLastSegment(RunSegment runSeg) {
		this.stopTime = runSeg.createdAt;
		this.runSegmentList.add(runSeg);
	}

	public long getTotalTime() {
		long totalTime = 0L;
		for (RunSegment runSeg : runSegmentList) {
			totalTime = totalTime + runSeg.time;
		}
		return totalTime;
	}

}
